console.log("i'm login ");

$(".tab_navs").find("li").click(function(){
      var index = $(this).index();
      console.log(index);
      $(this).addClass("active").siblings().removeClass('active');
     $(".tab_content>form").eq(index).show().siblings().hide();
      
  })
  
  ; (function () {
      var form = document.getElementById("form");
      var login = document.querySelector(".btn");
      var userInput = document.getElementById("username");
      var pwdInput = document.getElementById("password");
      var cookie_username = getCookie("username");
      var cookie_password = getCookie("password");
      var alert = document.querySelector(".alert");
      var loading = false;
      if (cookie_username && cookie_password) {
          userInput.value = cookie_username;
          pwdInput.value = cookie_password;
          // console.log( pwdInput.value);
         
          
      }
      form.onsubmit = function (evt) {
          var e = evt || window.event;
          e.preventDefault();
      }
      login.onclick = function () {
          if(loading) return false;
          var username = userInput.value;
          var password = pwdInput.value;
          loading = true;
          login.disabled = "disabled";
          login.innerHTML= "loading";
  
          xhrPost("http://47.107.82.140/yuznzhimin/login.php", { username, password }).then(res => {
              console.log(res);
              loading= false;
              login.removeAttribute("disabled");
              login.innerHTML = "登录";
              res = JSON.parse(res);
  
              if(res.statu === "success"){
                  showInfo("success","恭喜您登录成功！");
                  setTimeout(() => {
                      location.href = "./index.html";
                  }, 1000)
              }else if(res.statu === "error"){
                  showInfo("error",res.errorType);
                  
            }
            
  
          })
      }
      //提示的封装
      function showInfo(type,text){
          var children = alert.children[0];
          if(type === "success"){
              alert.className = "alert success";
          }else if(type === "error"){
              alert.className = "alert danger";
          }
          //显示内容
          children.innerHTML = text;
          alert.style.display = "block";
  
         
          setTimeout(() => {
              alert.style.display = "none";
          }, 3000)
  
    }
    
      function xhrPost(url, data) {
          return new Promise((resolve, reject) => {
              var xhr = new XMLHttpRequest();
              xhr.open("POST", url);
              xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
              var _data = [];
              for (let key in data) {
                  _data.push(`${key}=${data[key]}`);
              }
              xhr.send(_data.join("&"));
              xhr.onload = function () {
                  xhr.status === 200 ? resolve(xhr.responseText) : reject(xhr.status);
              }
          })
      }
      function getCookie(key) {
          var _cookie = document.cookie;
          // "key=value; key2=value; key3=value";
          var _cookie_item = _cookie.split("; ");
          var _key = [];
          var _value = _cookie_item.map(item => {
              var _temp = item.split("=");
              _key.push(_temp[0]);
              return _temp[1];
          })
          var index = _key.indexOf(key);
          if (index !== -1) {
              return _value[index];
          }
          return "";
      }
  })()

